﻿using Cdn.Blogger.Controllers;
using Cdn.Blogger.Models;
using System;
using System.Configuration;
using System.Web.Mvc;
using System.Web.Routing;
using XZen.Blogger;
namespace Cdn.Blogger.Controllers
{
    public class InitializationAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            BlogService service = (BlogService)filterContext.HttpContext.Session["BlogService.Instance"];
            if (service == null)
            {
                RouteValueDictionary routeDictionary = new RouteValueDictionary
				{

					{
						"action",
						"Login"
					},

					{
						"controller",
						"Home"
					}
				};
                filterContext.Result = new RedirectToRouteResult(routeDictionary);
            }
            else
            {
                UIController controller = filterContext.Controller as UIController;
                if (controller != null)
                {
                    controller.Service = service;
                }
                base.OnActionExecuting(filterContext);
            }
        }
    }

    public class    EnsureMagazineContextAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            MagazineService service = (MagazineService)filterContext.HttpContext.Session["MagazineService.Instance"];
            if (service == null && filterContext.ActionDescriptor.ActionName != "Login")
            {
                string ContentSetting = null;
                CDNSetting setting = new CDNSetting();
                setting.ContentUri = "http://localhost";
                setting.Password = "$Rijusmit2014";
                setting.Subscription = "blogger";
                setting.User = "rijusmit";
                setting.UserName = "rijusmit";

                if (ContentSetting == "ContentSetting")
                {
                    string connectoinstring = ConfigurationManager.AppSettings["ContentSetting"];
                    //CDNSetting setting = new CDNSetting();
                    setting.Parse(connectoinstring);
                    service = MagazineService.Create(setting.User, setting.Password, setting.ContentUri, setting.Subscription);
                    filterContext.HttpContext.Session["MagazineService.Instance"] = service;
                }
                else
                {
                    service = MagazineService.Create(setting.User, setting.Password, setting.ContentUri, setting.Subscription);
                    filterContext.HttpContext.Session["MagazineService.Instance"] = service;
                }
                if (service == null)
                {

                    RouteValueDictionary routeDictionary = new RouteValueDictionary
				{

					{
						"action",
						"Login"
					},

					{
						"controller",
						"Magazine"
					}
				};
                    filterContext.Result = new RedirectToRouteResult(routeDictionary);
                }
                else
                {
                    MagazineController controller = filterContext.Controller as MagazineController;
                    if (controller != null)
                    {
                        controller.Service = service;
                    }
                }
            }
            else
            {
                MagazineController controller = filterContext.Controller as MagazineController;
                if (controller != null)
                {
                    controller.Service = service;
                }
                base.OnActionExecuting(filterContext);
            }
        }
        
    }
}
